<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="这篇教程讲述如何创建最基本的Git服务器，最后会介绍如何限制通过Shell登录以确保安全。">
<meta property="og:type" content="article">
<meta property="og:title" content="搭建Git服务器">
<meta property="og:url" content="http://yoursite.com/2016/09/21/Other/搭建Git服务器/index.html">
<meta property="og:site_name" content="ZhouJiatao's Blog">
<meta property="og:description" content="这篇教程讲述如何创建最基本的Git服务器，最后会介绍如何限制通过Shell登录以确保安全。">
<meta property="og:updated_time" content="2017-01-13T04:46:12.189Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="搭建Git服务器">
<meta name="twitter:description" content="这篇教程讲述如何创建最基本的Git服务器，最后会介绍如何限制通过Shell登录以确保安全。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2016/09/21/Other/搭建Git服务器/"/>





  <title> 搭建Git服务器 | ZhouJiatao's Blog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  


<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-91431386-1', 'auto');
  ga('send', 'pageview');
</script>









  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">ZhouJiatao's Blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
    
      <p class="site-subtitle"></p>
    
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
  <link itemprop="mainEntityOfPage" href="http://yoursite.com/2016/09/21/Other/搭建Git服务器/">

  <span style="display:none" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="name" content="ZhouJiatao">
    <meta itemprop="description" content="">
    <meta itemprop="image" content="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg">
  </span>

  <span style="display:none" itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="ZhouJiatao's Blog">
    <span style="display:none" itemprop="logo" itemscope itemtype="http://schema.org/ImageObject">
      <img style="display:none;" itemprop="url image" alt="ZhouJiatao's Blog" src="">
    </span>
  </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                搭建Git服务器
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2016-09-21T11:40:49+00:00">
                2016-09-21
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Other/" itemprop="url" rel="index">
                    <span itemprop="name">Other</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/09/21/Other/搭建Git服务器/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2016/09/21/Other/搭建Git服务器/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          

          
          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>这篇教程讲述如何创建最基本的Git服务器，最后会介绍如何限制通过Shell登录以确保安全。</p>
<a id="more"></a>
<p>之前租用了DigitalOcean的服务器搭建VPN，以便翻墙查找技术资料(最低标准每个月才5美元)。既然有自己的服务器了，而且有20GB的硬盘空间，何不把Github的私人仓库都移到自己的服务器,这样每个月又可以省下7美元，而且创建的私人仓库数量不受限制。<br>所以，动工吧!</p>
<h1 id="创建SSH-Key"><a href="#创建SSH-Key" class="headerlink" title="创建SSH Key"></a>创建SSH Key</h1><pre><code>$ ls ~/.ssh
id_rsa        id_rsa.pub
</code></pre><p>已经存在id_rsa和id_rsa.pub文件,可以不用创建，继续使用原本的SSH Key。<br>当然，出于某种原因，你仍然想创建新的SSH Key，请先了解<a href="#anchor_1">如何管理多个SSH private Key</a></p>
<p>在OS X系统或者Linux系统下，创建SSH Key只需要在终端里输入,将<code>youremail</code>替换成你的邮箱地址：</p>
<pre><code>$ ssh-keygen -C &quot;youremail&quot;
</code></pre><p>按照终端的提示操作。</p>
<pre><code>Generating public/private rsa key pair.
Enter file in which to save the key (/home/example/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again: 
Your identification has been saved in example_rsa.
Your public key has been saved in example_rsa.pub.
The key fingerprint is:
ab:cd:ef:01:23:45:67:89:0a:bc:de:f0:12:34:56:78 youremail@XX.com &lt;youremail@xx.com&gt;
The key&apos;s randomart image is:
+--[ RSA 2048]----+
|    o+-+  ..     |
|  E o            |
|   . ++.o..      |
|    o o H .      |
|   . .   =       |
|    . =o.o=      |
| o .             |
|  .              |
|     = o  .      |
+-----------------+
</code></pre><p>终端输出类似以上内容时，SSH Key创建成功了。</p>
<p>如果你使用的是Windows系统，可以通过工具来创建SSH Key，比如 “PuTTY Gen”。</p>
<h1 id="服务器添加Git用户并且安装Git工具"><a href="#服务器添加Git用户并且安装Git工具" class="headerlink" title="服务器添加Git用户并且安装Git工具"></a>服务器添加Git用户并且安装Git工具</h1><p>以<strong>root</strong>身份登录你的服务器。<br>添加新用户”git”(用户名可按个人喜好选择)</p>
<pre><code># useradd git
</code></pre><p>为Git用户设置密码</p>
<pre><code># passwd git
</code></pre><p>安装git工具</p>
<ul>
<li>CentOS/Fedora: <code>yum install git</code></li>
<li>Ubuntu/Debian:  <code>apt-get install git</code></li>
</ul>
<h1 id="添加SSH-Key到服务器"><a href="#添加SSH-Key到服务器" class="headerlink" title="添加SSH Key到服务器"></a>添加SSH Key到服务器</h1><p>以<strong>Git</strong>用户的身份登陆服务器。从<strong>root</strong>身份切换到<strong>Git</strong>身份，用这条命令:</p>
<pre><code># su git
</code></pre><p>创建authorized_keys文件用于保存公钥。</p>
<pre><code>$ mkdir ~/.ssh &amp;&amp; touch ~/.ssh/authorized_keys
</code></pre><p>执行以上命令后，在/home/git/.ssh/目录下有authorized_keys文件。</p>
<p>现在将公钥文件(后缀为.pub的文件)的内容导入authorized_keys文件。<br>怎么导入？实际上，用文本编辑器打开公钥文件，拷贝里面的文本内容，复制到，authorized_keys文件内就可以。</p>
<p>你也可以在本地机器用以下命令完成上述操作:</p>
<pre><code>$ cat .ssh/id_rsa.pub | ssh git@123.xx.xx.xx &quot;cat &gt;&gt; ~/.ssh/authorized_keys&quot;
</code></pre><p>请将123.xx.xx.xx替换成你的服务器IP;</p>
<p>查看authorized_keys文件内容</p>
<pre><code>$ cat ~/.ssh/authorized_keys
</code></pre><h1 id="初始化Git仓库"><a href="#初始化Git仓库" class="headerlink" title="初始化Git仓库"></a>初始化Git仓库</h1><p>在服务器，进入想要保存git仓库的目录，执行(将‘my-project‘替换成你项目的名字):</p>
<pre><code>$ git init --bare my-project.git
</code></pre><p>在当前目录下，可以看到一个新的目录 my-project.git ,这就是我们创建的空的Git仓库。</p>
<p>Good! 现在你的VPS已经有了Git仓库。</p>
<h1 id="在本机使用Git服务"><a href="#在本机使用Git服务" class="headerlink" title="在本机使用Git服务"></a>在本机使用Git服务</h1><ol>
<li><p>如果本机没有该项目，就将服务器中的项目克隆到本机:</p>
<pre><code>$ git clone git@ip:my-project.git
</code></pre><p> 将<code>ip</code>替换成你的服务器ip地址。</p>
</li>
<li><p>如果本机已经有该项目，需要将本地的内容推送到服务器:</p>
<p> 进入项目所在目录，修改远程地址</p>
<pre><code>$ git remote set-url origin  git@ip:my-project.git
</code></pre><p> 将<code>ip</code>替换成你的服务器ip地址。</p>
<p> 推送本地内容到服务器</p>
<pre><code>$ git push -u origin master
</code></pre><p> 首次推送分支，加上 <code>-u</code> 参数，Git会将本地分支和远程分支关联起来，在以后的推送，都可以省略此参数。</p>
</li>
</ol>
<h1 id="附加"><a href="#附加" class="headerlink" title="附加"></a>附加</h1><h2 id="禁用shell登录"><a href="#禁用shell登录" class="headerlink" title="禁用shell登录"></a>禁用shell登录</h2><p>出于安全考虑，避免有人登录git用户修改其它目录的文件，我们不允许git用户登录shell，这可以通过编辑/etc/passwd文件完成。找到类似下面的一行：</p>
<p>git:x:1001:1001:,,,:/home/git:/bin/bash<br>改为：</p>
<p>git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell<br>这样，git用户可以正常通过ssh使用git，但无法登录shell，因为我们为git用户指定的git-shell每次一登录就自动退出。</p>
<p>－－－－－－</p>
<h2 id="如何管理多个SSH-private-Key"><a href="#如何管理多个SSH-private-Key" class="headerlink" title="如何管理多个SSH private Key"></a><a href="id:anchor_1" target="_blank" rel="external">如何管理多个SSH private Key</a></h2><p>允许电脑有多个不同的私钥，以连接不同的服务器。你需要编辑 <code>~/.ssh/config</code>:</p>
<pre><code>Host 10.10.10.1
IdentityFile ~/.ssh/linode_rsa

Host 200.20.20.2
IdentityFile ~/.ssh/id_rsa
</code></pre><p>如果连接10.10.10.1， 私钥<code>~/.ssh/linode_rsa</code>会被使用。</p>
<p>如果连接200.20.20.2， 私钥<code>~/.ssh/id_rsa</code>会被使用。</p>
<p>参考:<a href="https://www.mkyong.com/linux/multiple-ssh-private-keys-examples/" target="_blank" rel="external">Multiple SSH private keys Examples</a></p>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/08/28/iOS/iOS小问题汇总/" rel="next" title="iOS小问题汇总">
                <i class="fa fa-chevron-left"></i> iOS小问题汇总
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/10/23/iOS/iOS-Registering-for-Key-Value-Observing-KVO/" rel="prev" title="iOS Key-Value Observing(KVO)">
                iOS Key-Value Observing(KVO) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2016/09/21/Other/搭建Git服务器/"
           data-title="搭建Git服务器" data-url="http://yoursite.com/2016/09/21/Other/搭建Git服务器/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://7xr5jb.com1.z0.glb.clouddn.com/avatar%23privateinvestocat.jpg"
               alt="ZhouJiatao" />
          <p class="site-author-name" itemprop="name">ZhouJiatao</p>
          <p class="site-description motion-element" itemprop="description">Write simple code</p>
        </div>
        <nav class="site-state motion-element">
        
          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">29</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">18</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/RealMeZJT" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#创建SSH-Key"><span class="nav-number">1.</span> <span class="nav-text">创建SSH Key</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#服务器添加Git用户并且安装Git工具"><span class="nav-number">2.</span> <span class="nav-text">服务器添加Git用户并且安装Git工具</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#添加SSH-Key到服务器"><span class="nav-number">3.</span> <span class="nav-text">添加SSH Key到服务器</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#初始化Git仓库"><span class="nav-number">4.</span> <span class="nav-text">初始化Git仓库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#在本机使用Git服务"><span class="nav-number">5.</span> <span class="nav-text">在本机使用Git服务</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#附加"><span class="nav-number">6.</span> <span class="nav-text">附加</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#禁用shell登录"><span class="nav-number">6.1.</span> <span class="nav-text">禁用shell登录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何管理多个SSH-private-Key"><span class="nav-number">6.2.</span> <span class="nav-text">如何管理多个SSH private Key</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ZhouJiatao</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"realmezjt"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  












  
  

  

  

  

  


</body>
</html>
